const DB = require('../db/index');
const enumData = require('../enumeration/index');
const exception = require('../exception');
const fw = require('../core/framework');

module.exports = {
    create(data, successFn, failFn) {
        // todo 人脸表 什么是唯一的？ 特征值？
        try {
            DB.Face.create(data).then((res) => {
                successFn({code: 0,data:res})
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    update(data, successFn, failFn) {
        try {
            DB.Face.update(data).then(() => {
                successFn({code: 0})
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    search(data, successFn, failFn) {
        try {
            DB.Face.searchAll(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    delete(data, successFn, failFn) {
        this.findById(data.id, (dbRes) => {
            try {
                DB.Face.delete({id: data.id}).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }

        }, (e) => {
            e ? failFn(exception.ex(e)) : failFn(exception['1441'])
        });
    },
    findById(id, successFn, failFn) {
        try {
            DB.Face.search({id: id}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    },
    searchAllToAI( successFn, failFn){
        try {
            DB.Face.searchAllToAI().then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    getDataById(data, successFn, failFn) {
        let faceId = typeof data == 'object' ? data.face_id || data.faceId : data;
        try {
            DB.Face.getDataById({id: faceId}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    }
};
